package org.springblade.tms.module.demandorder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.common.utils.EnumUtils;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.tms.global.enums.TorTypeEnum;
import org.springblade.tms.module.demandorder.base.DemandListService;
import org.springblade.tms.module.demandorder.dto.AllocatingWaybillDto;
import org.springblade.tms.module.demandorder.dto.DemandOrderDto;
import org.springblade.tms.module.demandorder.entity.TMDemandOrder;
import org.springblade.tms.module.demandorder.vo.DemandOrderExcel;
import org.springblade.tms.module.demandorder.vo.DemandOrderVo;
import org.springblade.tms.module.demandorder.vo.TMDemandOrderVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.demandorder.controller
 * @className: DemandListController
 * @author: yr
 * @description: 需求单接口
 * @date: 2022/3/16 16:09
 */
@RestController
@RequestMapping("demandorder/DemandList")
@Api(value = "需求单-yr", tags = "需求单接口-yr")
public class DemandListController {
	@Resource
	private DemandListService demandListService;

	/**
	 * @param demandOrderDto:
	 * @param query:
	 * @return R<IPage < TMDemandOrderVO>>
	 * @author yr
	 * @description 需求单列表查询
	 * @date 2022/3/16 16:12
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "需求单列表查询", notes = "传入demandOrder")
	public R<IPage<TMDemandOrderVO>> list(DemandOrderDto demandOrderDto, Query query) {
		return demandListService.list(demandOrderDto, query);
	}

	/**
	 * @param id:
	 * @return null
	 * @author yr
	 * @description 需求单详情
	 * @date 2022/3/17 10:45
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "需求单详情", notes = "传入demandOrderId")
	public R<DemandOrderVo> detail(Long id, Query query) {
		return demandListService.detail(id, query);
	}


	/**
	 * @param tmId:
	 * @return null
	 * @author yr
	 * @description 司机车辆押运员列表
	 * @date 2022/3/17 10:45
	 */
	@GetMapping("/driverCarEscortList")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "司机车辆押运员坦克罐列表", notes = "传入承运商编码")
	public R<Object> driverCarEscortList(@ApiParam("承运商编码") String tmId, @ApiParam("需求单id") Long id, @ApiParam("运单id") Long transportId, Query query) {
		return demandListService.driverCarEscortList(tmId, id, transportId, query);
	}

	/**
	 * @param allocatingWaybillDto:
	 * @return null
	 * @author yr
	 * @description 分配运单
	 * @date 2022/3/17 10:45
	 */
	@PostMapping("/allocatingWaybill")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "分配运单", notes = "传入司机车辆押运员需求单id和物料id数量")
	public R allocatingWaybill(@RequestBody AllocatingWaybillDto allocatingWaybillDto) {
		return demandListService.allocatingWaybill(allocatingWaybillDto);
	}

	/**
	 * 删除 需求单
	 */
	@PostMapping("/invalid")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "需求单作废", notes = "传入id,作废原因")
	public R invalid(@RequestBody TMDemandOrder tmDemandOrder) {
		return demandListService.invalid(tmDemandOrder.getId(), tmDemandOrder.getReasonForVoiding());
	}

	/**
	 * @param demandOrderDto:
	 * @param query:
	 * @return R<IPage < TMDemandOrderVO>>
	 * @author yr
	 * @description 需求单导出
	 * @date 2022/3/16 16:12
	 */
	@GetMapping("/download")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "需求单导出", notes = "传入demandOrder")
	public void download(DemandOrderDto demandOrderDto, Query query, HttpServletResponse response) {
		query.setCurrent(-1);
		query.setSize(-1);
		List<TMDemandOrderVO> records = demandListService.list(demandOrderDto, query).getData().getRecords();
		List<DemandOrderExcel> demandOrderExcels = BeanUtil.copy(records, DemandOrderExcel.class);
		demandOrderExcels.forEach(demandOrderExcel -> {
			demandOrderExcel.setOrderType(EnumUtils.getEnumByValue(TorTypeEnum.class, demandOrderExcel.getOrderType()).get().description());
		});
		ExcelUtil.export(response, "TM门户需求单", "TM门户需求单", demandOrderExcels, DemandOrderExcel.class);
	}
}
